package com.itheima.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/*
使用Callable +FutrueTask
 */
public class Demo04 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable my = new MyCallable();
        FutureTask<String> task = new FutureTask<>(my);
        Thread t1 = new Thread(task,"小蔡");

        t1.start();

        FutureTask<String> task2 = new FutureTask<>(my);
        Thread t2 = new Thread(task2,"小黄");
        t2.start();
        String s = task.get();
        String s2 = task2.get();
        System.out.println("小蔡执行结果"+s);
        System.out.println("小黄执行结果"+s2);


    }
}
class  MyCallable implements Callable<String>{

    //想要让线程干什么 就在call中写对应的代码即可
    @Override
    public String call() throws Exception {
        Thread thread = Thread.currentThread();
        for (int i = 0; i < 100; i++) {
            Thread.sleep(500);
            System.out.println(thread.getName()+"中的"+i);
        }
        return "干完了";
    }
}